/**
 * 1，三方依赖独立打包
 * 2，非fas的合并生成一个包
 * 3，fas的合并生成一个包
 * @author dong.c
 */
import fs from 'fs-extra';
import { dirname,resolve } from 'path';
import { fileURLToPath } from 'url';
import { configs } from '../packages/t-pkgs/pkg.config.js'
import chalk from 'chalk'
import { rimraf } from 'rimraf'


const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

function pkg(files,pkgPath){

  let content = '';

  files.forEach(file => {
    content += fs.readFileSync(file, 'utf-8');
    content += '\n'
  });

  let file = resolve(__dirname, pkgPath);
  fs.ensureFileSync(file); // 确保文件存在，如果不存在则创建
  fs.writeFileSync(file, content);
}

rimraf.sync(resolve(__dirname, '../packages/t-pkgs/dist'))

for(let pkgConfig of configs){
 
  const pkgName = pkgConfig.pkgName
  console.log(`$ 生成${pkgName} `+chalk.green(`开始`))
  const pkgPath = `../packages/t-pkgs/dist/${pkgName}`
  pkg(pkgConfig.includeFiles,pkgPath)
  console.log(`$ 生成${pkgName} `+chalk.green(`完成`))

}


